package com.fruit.service.goods.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.goods.GoodsMapper;
import com.fruit.pojo.goods.Goods;
import com.fruit.pojo.goods.GoodsLable;
import com.fruit.pojo.goods.GoodsSpec;
import com.fruit.service.goods.GoodsManager;

@Service("goodsService")
public class GoodsService implements GoodsManager {

	@Autowired
	private GoodsMapper goodsMapper;
	
	/**
	 * 获取商品详细信息
	 * @param params
	 * @return
	 */
	@Override
	public Goods getGoodsDetail(Integer goodsId) {
		return goodsMapper.getGoodsDetail(goodsId);
	}

	/**
	 * 商品列表
	 * @param page
	 * @return
	 */
	@Override
	public List<Goods> getGoodsList(Integer classId) {
		return goodsMapper.getGoodsList(classId);
	}

	/**
	 * 查询一级分类
	 */
	@Override
	public List<Map<String, Object>> getOneClassName() {
		// TODO Auto-generated method stub
		return goodsMapper.getOneClassName();
	}
	
	/**
	 * 查询二级分类
	 */
	@Override
	public List<Map<String, Object>> getTwoClassName(Integer classId) {
		// TODO Auto-generated method stub
		return goodsMapper.getTwoClassName(classId);
	}

	/**
	 * 查询商品的规格
	 * @param classId
	 * @return
	 */
	@Override
	public List<GoodsLable> getFruitLableAttr(Integer id) {
		// TODO Auto-generated method stub
		List<GoodsLable> fruitLableList = goodsMapper.getFruitLableAttr(id);
		List<GoodsLable> goodsResult =  null;
		if (fruitLableList!=null ){
			if(fruitLableList.size()>0){
//			Map<String, GoodsLable> goodLableMap = new Hashmap<String, GoodsLable>(Collections.reverseOrder());
			Map<String,GoodsLable> goodLableMap=new LinkedHashMap<String,GoodsLable>();
			for (GoodsLable goodsLable : fruitLableList) {
				GoodsLable goodsLables = new GoodsLable();
				goodsLables.setAttrName(goodsLable.getAttrName());
				goodsLables.setSerial(goodsLable.getSerial());
				goodsLables.setId(goodsLable.getId());
				if (!goodLableMap.containsKey(goodsLable.getLableId() + "s")) {//不包含
					GoodsLable goodsLablesAttrNo = new GoodsLable();
					goodsLablesAttrNo.setLableId(goodsLable.getLableId());
					goodsLablesAttrNo.setLableName(goodsLable.getLableName());
					goodsLablesAttrNo.getGoodsList().add(goodsLables);
					goodLableMap.put(goodsLable.getLableId() + "s", goodsLablesAttrNo);
				} else {//包含
					GoodsLable goodsLablesAttrYes = (GoodsLable) goodLableMap.get(goodsLable.getLableId() + "s");
					goodsLablesAttrYes.getGoodsList().add(goodsLables); 
				}
			}
			Collection<GoodsLable> valueCollection = goodLableMap.values();
			goodsResult = new ArrayList<GoodsLable>(valueCollection);
			}else{
				
			}
		}else{
			
		}
		return goodsResult;
	}

	@Override
	public List<GoodsSpec> getGoodsForSpec(Integer id) {
		// TODO Auto-generated method stub
		return goodsMapper.getGoodsForSpec(id);
	}
	

}
